package com.onesports.common.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * @Author: HUANG GY.
 * @Date: 2020/6/19 10:18
 * @Description: 消息
 */
public class Message<T> implements Serializable {


    /**
     *  业务主键
     */
    private String bizCode ;

    /**
     *  消息id
     */
    private String msgId ;

    /**
     *  消息id
     */
    private T msgData ;

    /**
     *  消息创建时间 yyyy-MM-dd HH:mm:ss
     */
    private String createTime;

    /**
     *  版本号 默认为"1"
     */
    private String version;

    public Message() {
    }

    public Message(String msgId, T msgData, String createTime, String version) {
        this.msgId = msgId;
        this.msgData = msgData;
        this.createTime = createTime;
        this.version = version;
    }

    public Message(String bizCode, String msgId, T msgData, String createTime, String version) {
        this.bizCode = bizCode;
        this.msgId = msgId;
        this.msgData = msgData;
        this.createTime = createTime;
        this.version = version;
    }

    /**
     *  创建消息
     * @param t
     * @param <T>
     * @return
     */
    public static <T> String createMessage(T t){
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Message message = new Message(messageId,t,createTime,"1");
        String jsonString = JSON.toJSONString(message, SerializerFeature.WriteMapNullValue);
        return jsonString ;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public T getMsgData() {
        return msgData;
    }

    public void setMsgData(T msgData) {
        this.msgData = msgData;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }


    public String getBizCode() {
        return bizCode;
    }

    public void setBizCode(String bizCode) {
        this.bizCode = bizCode;
    }
}
